<%@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<div>
    <logic:present name="acc">
        <logic:equal name="roleUser" value="admin">
            <p align="right">role manager</p>
            <logic:present name="listRole">
                <logic:notEmpty name="listRole">
                    <table width="100%"  border="1">
                        <tr>
                            <td width="50%">
                                <table width="100%" border="1">
                                    <tr>
                                        <td>role id</td>
                                        <td>role name</td>
                                        <td>action</td>
                                    </tr>
                                    <logic:iterate name="listRole" id="listR">
                                        <tr>
                                            <td><bean:write name="listR" property="roleId"/></td>
                                            <td><bean:write name="listR" property="roleName"/></td>
                                            <td height="39" colspan="3"><div align="center">
                                                    <img title="Add" src="admin/img/action_add.png"  style="cursor:pointer">
                                                    <img title="Edit" src="admin/img/action_remove.png"  style="cursor:pointer" onclick="loadXMLDoc('roleEdit','AdminUser.do?action=roleEdit&roleId=<bean:write name="listR" property="roleId"/>')">
                                                </div>
                                            </td>
                                        </tr>
                                    </logic:iterate>

                                </table>

                            </td>
                        </tr>
                    </table>
                    <div id="roleEdit">

                    </div>

                </logic:notEmpty>
                <logic:empty name="listRole">
                    ban chi dc xem thong tin ma thoi
                </logic:empty>
            </logic:present>
        </logic:equal>
        <logic:notPresent name="listRole">
            ban chi dc xem thong tin ma thoi
        </logic:notPresent>
    </logic:present>
    <logic:notPresent name="acc">
        <jsp:forward page="adminHome.jsp"/>
    </logic:notPresent>
</div>
<script>
    function loadXMLDoc(id,element)
    {
        if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                document.getElementById(id).innerHTML=xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET",element,true);
        xmlhttp.send();
    }
    function showAdd(){
        document.getElementById('fr1roleId').value ="";
        document.getElementById('fr1roleName').value ="";
        document.getElementById('fr1roleId').disabled= false;
        document.fr1.style.visibility='visible';
        for(i = 0;i < document.fr1.func.length;i++){
            document.fr1.role[i].checked = false;
        }
        document.fr1.func[0].checked = true;
        document.getElementById('update').style.visibility='hidden';
        document.getElementById('delete').style.visibility='hidden';
        

    }

    function showUpdate(element){
        for(i = 0;i < document.fr1.func.length;i++){
            if(document.fr1.func[i].value == document.getElementById('frroleId'+element).value){
                document.fr1.func[i].checked = true;
            }

        }
        document.fr1.style.visibility='visible';
        document.getElementById('insert').style.visibility='hidden';
        document.getElementById('update').style.visibility='visible';
        document.getElementById('delete').style.visibility='hidden';
        document.getElementById('fr1use').disabled= true;
        document.getElementById('fr1useedit').value = document.getElementById('fruse'+element).value;
        document.getElementById('fr1use').value = document.getElementById('fruse'+element).value;
        document.getElementById('fr1name').value = document.getElementById('frname'+element).value;
        document.getElementById('fr1mail').value = document.getElementById('frmail'+element).value;
        document.getElementById('fr1phone').value = document.getElementById('frphone'+element).value;
        document.getElementById('fr1add').value = document.getElementById('fradd'+element).value;
    }

    function showDelete(element){
        for(i = 0;i < document.fr1.role.length;i++){
            if(document.fr1.role[i].value == document.getElementById('frroleId'+element).value){
                document.fr1.role[i].checked = true;
            }

        }
        document.fr1.style.visibility='visible';
        document.getElementById('insert').style.visibility='hidden';
        document.getElementById('delete').style.visibility='visible';
        document.getElementById('update').style.visibility='hidden';
        document.getElementById('fr1use').disabled= true;
        document.getElementById('fr1useedit').value = document.getElementById('fruse'+element).value;
        document.getElementById('fr1use').value = document.getElementById('fruse'+element).value;
        document.getElementById('fr1name').value = document.getElementById('frname'+element).value;
        document.getElementById('fr1mail').value = document.getElementById('frmail'+element).value;
        document.getElementById('fr1phone').value = document.getElementById('frphone'+element).value;
        document.getElementById('fr1add').value = document.getElementById('fradd'+element).value;
    }

    function emailValidator(elem, helperMsg){
        var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
        if(elem.value.match(emailExp)){
            return true;
        }else{
            alert(helperMsg);
            elem.focus();
            return false;
        }
    }
    function notEmpty(elem, helperMsg){
        if(elem.value.length == 0){
            alert(helperMsg);
            elem.focus(); // set the focus to this input
            return false;
        }
        return true;
    }
    function validatorForm(){
        var use = document.getElementById('fr1use');
        var name = document.getElementById('fr1name');
        var email = document.getElementById('fr1mail');
        var phone = document.getElementById('fr1phone');
        var pass = document.getElementById('fr1pass');
        var add = document.getElementById('fr1add');

        // Check each input in the order that it appears in the form!
        if(notEmpty(use, "Please enter usename")){
            if(notEmpty(pass, "Please enter password")){
                if(notEmpty(name, "Please enter full name")){
                    if(notEmpty(email, "Please enter fmail;")){
                        if(emailValidator(email, "Please check mail")){
                            if(notEmpty(phone,"Please enter phone")){
                                if(notEmpty(add, "Please enter address")){

                                    return true;

                                }
                            }
                        }
                    }
                }
            }
        }
        return false;
    }
    function confirmForm(){
        var answer = confirm("are you want delete")
        if (answer){

            return true;
        }
        else{
            return false;
        }
    }
</script>